<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="alipayOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="支付宝类目代码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['alipayNo',{rules: [{ required: true, validator:numberValidator }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>

          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="支付宝类目描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['apFstName',{rules: [{ required: true, validator:lengthValidator }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="支付宝二级类目描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['apSecName',{rules: [{ required: true, validator:lengthValidator }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="支付宝三级类目编码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['apThiCode',{rules: [{ required: true, validator:numberValidator }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="支付宝三级类目描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['apThiName',{rules: [{ required: true, validator:lengthValidator }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.title !== '查看支付宝类目'">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'AlipayOperation',
    props: {
      alipayOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增支付宝类目',
        isStop: false,
        isCancel: false,
        isUpd: false,
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',

        info: {},

      }
    },
    computed: {
      show: {
        get: function () {
          return this.alipayOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      lengthValidator(rule,value,callback){
        if(!value){
          callback(new Error('必填'))
        }else {
          callback(value ? ((/^.{1,20}$/).test(value) ? undefined : new Error('请重新输入')) : undefined)
        }
      },
      numberValidator(rule,value,callback){
        if(!value){
          callback(new Error('必填'))
        }else {
          callback(value ? ((/^\d{1,20}$/).test(value) ? undefined : new Error('请重新输入')) : undefined)
        }
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      reset() {
        this.loading = false
        this.isCancel = false
        this.isUpd = false
        this.isStop = false
        this.title = '新增支付宝类目'
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增支付宝类目') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改支付宝类目') {
              this.info.optFlag = 'upd'
            } else if (this.title == '注销支付宝类目') {
              this.info.optFlag = 'cancel'
            }

            if (this.title == '新增支付宝类目') {
              this.$post('/admin/merParam/alipayInfo', {
                ...this.info
              }).then((r) => {
                this.reset()
                if(r.data.code == '100000'){
                  this.$emit('success')
                }else {
                  this.$message.error(r.data.message)
                }

              }).catch(() => {
                this.loading = false
              })
            } else if (this.title == '修改支付宝类目') {
              this.$put('/admin/merParam/alipayInfo/update', {
                ...this.info
              }).then((r) => {
                this.reset()
                if(r.data.code == '100000'){
                  this.$emit('success')
                }else {
                  this.$message.error(r.data.message)
                }
              }).catch(() => {
                this.loading = false
              })
            } else {
              this.$put('/admin/merParam/alipayInfo/cancel', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            }

          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['id', 'alipayNo', 'apFstName', 'apSecName', 'apThiCode', 'apThiName'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setAlipayFormValues({...info}) {
        let fields = ['id', 'alipayNo', 'apFstName', 'apSecName', 'apThiCode', 'apThiName']
        if (info.operation == 'cancel') {
          this.isCancel = true
          this.isUpd = true
          this.title = '注销支付宝类目'
        } else if (info.operation == 'view') {
          this.isUpd = true
          this.title = '查看支付宝类目'
        } else {
          this.title = '修改支付宝类目'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>




